/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

define(['adminapp', 'core/datatable'], function ($app) {
    $app.navactive('member');

    var doms = {
        action   : '#action_template',
        modal    : '#modal_template',
        datatable: '#datatable_members'
    };
    var action_tpl = Handlebars.compile($(doms.action).html());


    var member_grid = new Datatable();
    member_grid.init({
        src      : $(doms.datatable),
        onSuccess: function (grid) {
        },
        onError  : function (grid) {
        },
        dataTable: {
            'bLengthChange': false,
            'sAjaxSource'   : g.ctx + 'customer/member/list',
            'aaSorting'     : [
                [ 1, "asc" ]
            ],
            'aoColumns'     : [
                $app.checkall,
                $app.autono,
                {'mDataProp': 'id' },
                {'mDataProp': 'name' },
                {'mDataProp': 'card_no' },
                {'mDataProp': 'id_card' },
                {'mDataProp': 'phone' },
                {'mDataProp': 'nationality' },
                {'mDataProp': 'gender' },
                {'mDataProp': 'postcode' },
                {'mDataProp': 'mail_address' }
            ],
            'aoColumnDefs'  : [
                { 'bSearchable': false, 'bVisible': false, 'aTargets': [ 2 ] },
                {
                    'aTargets': [6],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        var gender = full['gender'];
                        if (gender == 0) {
                            return '<span class="label label-default">女</span>';
                        } else if (gender == 1) {
                            return '<span class="label label-primary">男</span>';
                        } else {
                            return '<span class="label label-success">未知</span>';
                        }
                    }
                },
                {
                    'aTargets': [11],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        return action_tpl(full);
                    }
                }
            ],
            'fnRowCallback' : function (nRow, aData, iDisplayIndex) {
                var oSettings = member_grid.getDataTable().fnSettings();
                $("td:eq(1)", nRow).html(oSettings._iDisplayStart + iDisplayIndex + 1);
                return nRow;
            }
        }
    });

    var modal = $(doms.modal).html();

    var re = /^-?\d+\.?\d{0,1}$/;

    $('#data-table').on( 'click','a[data-hook="ticket"]', function (e) {
        var $this = $(this);
        var member = $this.attr('data-member');
        if (member) {
            bootbox.dialog({
                message: modal,
                title  : "小票积分",
                buttons: {
                    success: {
                        label    : "确定",
                        className: "btn-success",
                        callback : function () {
                            var $body = $('.modal-body');
                            var ticketNoEl = $body.find('input[name="ticketNo"]');
                            var integralEl = $body.find('input[name="integral"]');
                            var ticketNo = ticketNoEl.val();
                            var integral = integralEl.val();
                            if (!ticketNo) {
                                ticketNoEl.parent().find('.error').remove();
                                ticketNoEl.parent().append('<span class="error help-inline" style="color: #b94a48;">请输入小票号</span>');
                                ticketNoEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!integral) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append('<span class="error help-inline" style="color: #b94a48;">请输入积分</span>');
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!re.test(integral)) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append('<span class="error help-inline" style="color: #b94a48;">请输入合法数值</span>');
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            var b = false;
                            $.ajax({
                                url : g.ctx + 'customer/member/ticker',
                                data: { member: member, integral: integral, ticket: ticketNo},
                                async: false,
                                success: function(data) {
                                    if (data.status == 'OK') {
                                        //window.location.reload();
                                        member_grid.getTable().fnDraw();
                                        b = true;
                                    } else {
                                        ticketNoEl.parent().find('.error').remove();
                                        ticketNoEl.parent().append('<span class="error help-inline" style="color: #b94a48;">',data.message,'</span>');
                                        ticketNoEl.css('border-color', '#b94a48');
                                        b = false;
                                    }
                                }
                            });
                            return b;
                        }
                    },
                    danger : {
                        label    : "取消",
                        className: "btn-danger",
                        callback : function () {
                            //Example.show("uh oh, look out!");
                        }
                    }
                }
            });
        }
        return false;
    });



    return {};
});